<?php

class AppController extends Controller {
	public $components = array('session','acl','auth','captcha');
	public $my = null;	
	public function beforeFilter() {
        //Configure AuthComponent
        $this->C_auth->authorize = 'actions';
        $this->C_auth->loginAction = '/user/login';
        $this->C_auth->logoutRedirect = '/user/login';
        $this->C_auth->loginRedirect = '/default/index';
		$this->C_auth->ajaxLogin = '/elements/users/expired_login';
		
		$this->C_auth->allowedActions = array('display');
/*		$this->C_auth->actionPath = 'controllers/';
  		$this->_lockSiteURL(Param::read('siteURL')); */		
		$this->_getMyInfo(); 
    }

  	public function _getMyInfo() {
	    $myifo = array();
		if ($this->C_auth->user()) {
			$myID = $this->C_auth->user('id');			
			$sql ='[@@:UM user][@@:QU select][_select user.* ][_from][_table user:yes][_where {%_where%}][_limit 1]';			
			$myInfo = POP::exe($sql,array('user.id' => $myID));		
			$myifo = $myInfo[0]['user'];
			foreach ($myInfo[0]['user'] as $key => $value) {
				$this->my->$key = $value;
			}
		} else {
			$this->my = null;
		}
		$this->set('myifo',$myifo);
	} 	
	

	
  	public function _teamRights() {		
        if($this->my->team_pwd) {
            if($this->C_session->read('_user_temp.team.team_pwd') && $this->C_session->read('_user_temp.team.team_pwd')=='yes') {
                return 1;
            }			
		    if(!$this->C_session->read('_user_temp.team.login') || $this->C_session->read('_user_temp.team.login')=='no') {
		        $this->C_session->write('_user_temp.team.login', 'no');
			    $this->flash("员工账号还未登入，请先登入！或用最高管理员密码登入!", Router::url('team/login/'));
		    }else {		    
		        if(!$this->C_session->read('_user_temp.team.rights')) {
		            $this->flash("员工账号还未登入，请先登入！或用最高管理员密码登入!", Router::url('team/login/'));
		        } else {
			        $rights = $this->C_session->read('_user_temp.team.rights');
		            if(in_array($this->name.'/'.$this->action,$rights)) {
				        $this->flash("你的员工账号无权限进行此操作,请切换你的员工账号！或用最高管理员密码登入!",Router::url('team/login/'));
				    }
				    return 1 ;
		        }
            }
        }else {
		    $this->flash("此操作不能进行，请先设置最高管理员密码！", Router::url('team/teampwd/'));
		}
	} 

	public function fpage($tb = array()) {
        $tb = $tb + array('model' => '', 'joins' => array(),'fields' => array('*'),
					'conditions' => '',	'order' => '',
					'limit' => '', 'group' => '','url' => '',
					'page' => 1,'plist' => 6,'startPage' => true					
					) ; 		
        extract($tb, EXTR_SKIP);
 		$data = array();
		if($startPage) {

            $page = empty($page) ? 1 : intval($page);
			$plist = empty($plist) ? 6 : intval($plist);
			$query = array(
					'conditions' => $conditions, 
					'fields' => array(' COUNT(*) AS count '), 
					'joins' => $joins, 
					);	
			$data1 = POP::exe('[@@:UM '.$model.'][@@:QU find:first]',$query);
		    $totalnum = $data1[0]['count'];		
	        $totalpage=intval(@ceil($totalnum/$limit));
	        $nextpage=$page+1;
	        $uppage=$page-1;		 
	        if($nextpage>$totalpage){
		        $nextpage=$totalpage;
	        }		 
	        if($uppage<1){
		        $uppage=1;
	        }
	        $s=$page-3;
	        if($s<1){
		       $s=1;
	        }
	        $b=$s + $plist;
	        if($b>$totalpage){
		        $b=$totalpage;
	        }			
	        for($j=$s;$j<=$b;$j++){
		        if($j==$page) {
			        $data['page']['current']['num'] = $j;
					$data['page']['current']['url'] = "$url&page=$j";
		        } else {      
			        $data['page']['plist'][$j] = "$url&page=$j";			         
		        }
	        }		  
            if(!isset($data['page']['current'])){
		        $data['page']['current']['num'] =1;
		        $data['page']['current']['url'] = "$url&page=1"; 
		    }	  
		    $start = ($page-1) * $limit;		  	  
		    $data['page']['head'] =  "$url&page=1";
		    $data['page']['prevPage'] = "$url&page=$uppage";
		    $data['page']['nextPage'] = "$url&page=$nextpage";
		    $data['page']['foot'] = "$url&page=$totalpage";
		    $data['page']['pageCount'] = $totalpage;
		    $data['page']['total'] = $totalnum;
		}else {
		    $start = 0;
		}
		if($start) $limit = " {$start},{$limit} ";
		$query = array(
				'conditions' => $conditions, 
				'fields' => $fields, 
				'joins' => $joins, 
				'limit' => $limit,
				'offset' => $start, 
				'order' => $order, 
				'group' => $group
				);	
	    $data['data'] = POP::exe('[@@:UM '.$model.'][@@:QU find:all]',$query);	
        return $data ;
	}		
	
}
?>